package com.dure.hotmemory.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dure.commonlib.util.ToastUtils;
import com.dure.hotmemory.MainActivity;
import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.listeners.DialogCloseListener;
import com.dure.hotmemory.listeners.SelectPictureListener;
import com.socks.library.KLog;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by leix on 30/05/2017.
 */

public class DialogUtil {

    public static void showUpdateDialog(final MainActivity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.content_tv);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmTV = (TextView) view.findViewById(R.id.confirm_tv);
        ImageView adIV = (ImageView) view.findViewById(R.id.ad_iv);
        adIV.setVisibility(View.GONE);

        titleTV.setText(R.string.version_update);
        contentTV.setText(R.string.update_content);

        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmListener.confirmed(null);
                dialog.cancel();
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    public  static void showExitDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.content_tv);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmTV = (TextView) view.findViewById(R.id.confirm_tv);
        ImageView adIV = (ImageView) view.findViewById(R.id.ad_iv);

        titleTV.setText(R.string.exit_warning);
        contentTV.setText(R.string.exit_content);
        adIV.setVisibility(View.GONE);

        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        confirmTV.setText("退出");
        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                if (confirmListener != null) {
                    confirmListener.confirmed(null);
                }
            }
        });


        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    // 显示支付对话框
    public static void showPayDialog(final Activity context, final ConfirmListener confirmListener) {
        String title = "开启vip";
        String content = "付费后您可以自定义编码，上传训练数据进行永久保存，还可以进行挑战训练。";
        String leftContext = "稍后";
        String rightContext = "开启(￥50)";
        showTowBtnDialog(context, title, content, leftContext, rightContext, false, false, new DialogCloseListener() {
            @Override
            public void deniedClose(Bundle bundle) {

            }

            @Override
            public void confirmedClose(Bundle bundle) {
                if (confirmListener != null) {
                    bundle = new Bundle();
                    bundle.putString("amount", "50");
                    confirmListener.confirmed(bundle);
                }
            }
        });
    }

    public static void showTowBtnDialog(final Activity context, String title, String content) {
        showTowBtnDialog(context, title, content, "", "", false, false, null);
    }

    public static void showTowBtnDialog(final Activity context, String title, String content,
                                        final DialogCloseListener dialogCloseListener) {
        showTowBtnDialog(context, title, content, "", "", false, false, dialogCloseListener);
    }

    public static void showTowBtnDialog(final Activity context, String title, String content,
                                        boolean showClose, boolean showInput,
                                        final DialogCloseListener dialogCloseListener) {
        showTowBtnDialog(context, title, content, "", "", showClose, showInput, dialogCloseListener);
    }

    public static void showTowBtnDialog(Activity context, String title, String content, String leftText,
                                        String rightText,DialogCloseListener dialogCloseListener){
        showTowBtnDialog(context, title, content, leftText, rightText, false, false,dialogCloseListener);
    }

    public static void showTowBtnDialog(final Activity context, String title, String content, String leftText,
                                        String rightText, boolean showClose, boolean showInput,
                                        final DialogCloseListener dialogCloseListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.content_tv);
        TextView closeBtn = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmBtn = (TextView) view.findViewById(R.id.confirm_tv);
        ImageView adIV = (ImageView) view.findViewById(R.id.ad_iv);
        ImageView closeIV = (ImageView) view.findViewById(R.id.close_iv);
        final EditText inputET = (EditText) view.findViewById(R.id.content_et);


        if (showInput)
            inputET.setVisibility(View.VISIBLE);

        adIV.setVisibility(View.GONE);
        if (showClose) {
            closeIV.setVisibility(View.VISIBLE);
            closeIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.cancel();
                    if (dialogCloseListener != null) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("btn_index", 0);
                        dialogCloseListener.deniedClose(bundle);
                    }
                }
            });
        }

        titleTV.setText(title);
        contentTV.setText(content);

        if (!TextUtils.isEmpty(leftText)) {
            closeBtn.setText(leftText);
        }

        if (!TextUtils.isEmpty(rightText)) {
            confirmBtn.setText(rightText);
        }

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                if (dialogCloseListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("btn_index", 1);
                    dialogCloseListener.deniedClose(bundle);
                }
            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (dialogCloseListener != null) {
                    Bundle bundle = new Bundle();
                    if (inputET.getVisibility() == View.VISIBLE) {
                        String content = inputET.getText().toString();
                        if (!TextUtils.isEmpty(content))
                            bundle.putString("input", content);
                        else {
                            ToastUtils.show(context, "输入内容不能为空");
                            return;
                        }
                    }
                    dialogCloseListener.confirmedClose(bundle);
                }
                dialog.cancel();
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    // 显示分享后就能拥有计划功能
    public static void showShareDialog(final Activity context) {
        showShareDialog(context, null);
    }

    // 显示分享后就能拥有计划功能
    public static void showShareDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.content_tv);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmTV = (TextView) view.findViewById(R.id.confirm_tv);
        ImageView adIV = (ImageView) view.findViewById(R.id.ad_iv);
        adIV.setVisibility(View.GONE);

        titleTV.setText("开启训练计划");
        // 如果是老用户,提示只分享一次, 要将不会再出现
        contentTV.setText("只需分享一次应用, 您就可以永久拥有自定义训练计划的功能哦");

        cancelTV.setText("取消");
        confirmTV.setText("分享");

        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
//                ShareUtil.showCustomShareLayout(context, confirmListener);

            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    // 显示分享后就能拥有计划功能
    public static void showShareDialog(final Activity context, String title, String content) {
        showShareDialog(context, null, title, content, null);
    }

    // 显示分享后就能拥有计划功能
    public static void showShareDialog(final Activity context, final Planet planet, final String title, final String content, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.content_tv);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmTV = (TextView) view.findViewById(R.id.confirm_tv);
        ImageView adIV = (ImageView) view.findViewById(R.id.ad_iv);
        adIV.setVisibility(View.GONE);

        titleTV.setText(title);
        // 如果是老用户,提示只分享一次, 要将不会再出现
        contentTV.setText(content);

        cancelTV.setText("取消");

        if (TextUtils.equals(title, "分享"))
            confirmTV.setText("分享");
        else {
            confirmTV.setText("打分");
        }

        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
//                ShareUtil.showCustomShareLayout(context, planet, confirmListener);
                if (TextUtils.equals(title, "分享"))
                    ShareUtil.showCustomShareDialog(context, planet, confirmListener);
                else {
                    ShareUtil.goToMarket(context, context.getPackageName());
                }

            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    static TextView lastSelectedView;
    public static void showRewardDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.reward_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView fiveRMBTV = (TextView) view.findViewById(R.id.five_rmb_tv);
        TextView tenRMBTV = (TextView) view.findViewById(R.id.ten_rmb_tv);
        TextView twentyRMBTV = (TextView) view.findViewById(R.id.twenty_rmb_tv);
        TextView fiftyRMBTV = (TextView) view.findViewById(R.id.fifty_rmb_tv);
        TextView oneHundredRMBTV = (TextView) view.findViewById(R.id.onehundred_rmb_tv);
        TextView otherRMBTV = (TextView) view.findViewById(R.id.other_rmb_tv);
        TextView confirmTV = (TextView) view.findViewById(R.id.confirm_tv);
        TextView cancelTV = (TextView) view.findViewById(R.id.cancel_tv);
        final EditText moneyET = (EditText) view.findViewById(R.id.money_et);
        final Bundle bundle = new Bundle();
        bundle.putString("amount", "5");

        lastSelectedView = fiveRMBTV;

        cancelTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (confirmListener != null) {
                    if (TextUtils.equals(bundle.getString("amount"), "other")) {
                        String amount = moneyET.getText().toString();
                        if (TextUtils.isEmpty(amount)) {
                            ToastUtils.show(context, "请输入金额");
                        } else if (Float.valueOf(amount) == 0) {
                            ToastUtils.show(context, "输入金额必须大于0");
                        } else {
                            dialog.cancel();
                            bundle.putString("amount", amount);
                            confirmListener.confirmed(bundle);
                        }

                    } else {
                        dialog.cancel();
                        confirmListener.confirmed(bundle);
                    }
                }
            }
        });

        fiveRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "5");
            }
        });

        tenRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "10");
            }
        });

        twentyRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "20");
            }
        });

        fiftyRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "50");
            }
        });

        oneHundredRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "100");
            }
        });

        otherRMBTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeSelectedView(view);
                bundle.putString("amount", "other");
                moneyET.setVisibility(View.VISIBLE);
            }
        });


        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    private static void changeSelectedView(View view) {
        lastSelectedView.setBackgroundResource(R.drawable.transparent_arc_stroke_drawable);
        view.setBackgroundResource(R.drawable.airblue_arc_stroke_drawable);
        lastSelectedView = (TextView) view;
    }

    public static void showNativeLoginDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.native_login_layout, null);
        final TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
//        final TextView registBtn = (TextView) view.findViewById(R.id.regist_btn);
        final TextView loginBtn = (TextView) view.findViewById(R.id.login_btn);
        final TextView confirmBtn = (TextView) view.findViewById(R.id.confirm_btn);
        ImageView closeIV = (ImageView) view.findViewById(R.id.close_iv);
        final ImageView backIV = (ImageView) view.findViewById(R.id.back_iv);

        final LinearLayout loginRegistLL = (LinearLayout) view.findViewById(R.id.login_regist_ll);
        final LinearLayout findLL = (LinearLayout) view.findViewById(R.id.find_ll);
        final LinearLayout otherLoginLL = (LinearLayout) view.findViewById(R.id.other_login_ll);

        final TextView findPwdBtn = (TextView) view.findViewById(R.id.find_pwd_tv);

        final EditText phoneET = (EditText) view.findViewById(R.id.phone_et);
        final EditText passwordET = (EditText) view.findViewById(R.id.password_et);
        final RelativeLayout verifyLL = (RelativeLayout) view.findViewById(R.id.verify_ll);
        final EditText codeET = (EditText) view.findViewById(R.id.code_et);
        final TextView timeGetTV = (TextView) view.findViewById(R.id.time_get_tv);
        verifyLL.setVisibility(View.GONE);


        backIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                titleTV.setText("登录");
                backIV.setVisibility(View.GONE);
                loginRegistLL.setVisibility(View.VISIBLE);
                findLL.setVisibility(View.GONE);
                passwordET.setHint("请输入密码");
//                registBtn.setBackgroundResource(R.drawable.transparent_arc_graystroke_drawable);
                loginBtn.setBackgroundResource(R.drawable.transparent_arc_stroke_drawable);
                findPwdBtn.setVisibility(View.VISIBLE);
                verifyLL.setVisibility(View.GONE);
            }
        });

        findPwdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                titleTV.setText("找回密码");
                backIV.setVisibility(View.VISIBLE);
                loginRegistLL.setVisibility(View.GONE);
                findLL.setVisibility(View.VISIBLE);
                passwordET.setHint("请输入新密码");
                findPwdBtn.setVisibility(View.GONE);

                verifyLL.setVisibility(View.VISIBLE);


            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String phoneStr = phoneET.getText().toString();
                String passwordStr = passwordET.getText().toString();
                String codeStr = codeET.getText().toString();

                doLoginOrRegist(context, dialog, "find", phoneStr, passwordStr, codeStr, confirmListener);
            }
        });

        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

//        registBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                titleTV.setText("注册");
//                registBtn.setBackgroundResource(R.drawable.transparent_arc_bluestroke_drawable);
//                loginBtn.setBackgroundResource(R.drawable.transparent_arc_graystroke_drawable);
//                String phoneStr = phoneET.getText().toString();
//                String passwordStr = passwordET.getText().toString();
//                String codeStr = codeET.getText().toString();
//                findPwdBtn.setVisibility(View.GONE);
//                otherLoginLL.setVisibility(View.GONE);
//
//                if (verifyLL.getVisibility() == View.VISIBLE)
//                    doLoginOrRegist(context, dialog, "regist", phoneStr, passwordStr, codeStr, confirmListener);
//                else {
//                    verifyLL.setVisibility(View.VISIBLE);
//                }
//            }
//        });

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                titleTV.setText("登录");
//                registBtn.setBackgroundResource(R.drawable.transparent_arc_graystroke_drawable);
                loginBtn.setBackgroundResource(R.drawable.transparent_arc_bluestroke_drawable);
                String phoneStr = phoneET.getText().toString();
                String passwordStr = passwordET.getText().toString();
                findPwdBtn.setVisibility(View.VISIBLE);
                otherLoginLL.setVisibility(View.VISIBLE);

                if (verifyLL.getVisibility() == View.GONE)
                    doLoginOrRegist(context, dialog, "login", phoneStr, passwordStr, "", confirmListener);
                else {
                    verifyLL.setVisibility(View.GONE);
                }
            }
        });

        otherLoginLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                showThirdLoginDialog(context, confirmListener);
            }
        });

        // 获取验证码
        timeGetTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    String phoneStr = phoneET.getText().toString();
                    if (TextUtils.isEmpty(phoneStr)) {
                        ToastUtils.show(context, "手机号不能为空");
                        return;
                    } else if (phoneStr.length() != 11) {
                        ToastUtils.show(context, "请输入正确的手机号");
                        return;
                    }
                    time = 60;
                    timeGetTV.setEnabled(false);
                    Timer timer = new Timer();
                    TimerTask timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            KLog.d("lesleixz", "timer count");
                            if (time > 0) {
                                time--;

                                context.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        timeGetTV.setText(time + "s");
                                    }
                                });
                            } else {

                                cancel();

                                context.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        timeGetTV.setEnabled(true);
                                        timeGetTV.setText("重新获取");
                                    }
                                });
                            }

                        }
                    };

                    timer.scheduleAtFixedRate(timerTask, 0, 1000);

                    Bundle bundle = new Bundle();
                    bundle.putString("phone", phoneStr);
                    bundle.putString("type", "code");
                    confirmListener.confirmed(bundle);
                }
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.5f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    private static void doLoginOrRegist(Context context, Dialog dialog, String type, String phoneStr, String passwordStr, String codeStr, ConfirmListener confirmListener) {

        if (TextUtils.isEmpty(phoneStr)) {
            ToastUtils.show(context, "手机号不能为空");
            return;
        } else if (phoneStr.length() != 11) {
            ToastUtils.show(context, "请输入正确的手机号");
            return;
        }

        if (TextUtils.isEmpty(passwordStr)) {
            ToastUtils.show(context, "密码不能为空");
            return;
        } else if (passwordStr.length() < 6) {
            ToastUtils.show(context, "密码长度不能小于6");
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putString("phone", phoneStr);
        bundle.putString("password", passwordStr);
        if (TextUtils.equals(type, "login")) {// 执行登录操作
            if (confirmListener != null) {
                confirmListener.confirmed(bundle);
                dialog.cancel();
            }

        } else {
            if (TextUtils.isEmpty(codeStr)) {
                ToastUtils.show(context, "请输入验证码");
                return;
            } else {// 执行注册/找回/绑定操作
                if (confirmListener != null) {
                    bundle.putString("code", codeStr);
                    confirmListener.confirmed(bundle);
                    dialog.cancel();
                }
            }
        }
    }

    public static void showThirdLoginDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.third_login_layout, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView closeBtn = (TextView) view.findViewById(R.id.close_btn);

        LinearLayout qqLL = (LinearLayout) view.findViewById(R.id.qq_ll);
        LinearLayout sinaLL = (LinearLayout) view.findViewById(R.id.sina_ll);
        LinearLayout wxLL = (LinearLayout) view.findViewById(R.id.wx_ll);

        qqLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                if (confirmListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("login_type", SHARE_MEDIA.QQ.toString());
                    confirmListener.confirmed(bundle);
                }
            }
        });

        sinaLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                if (confirmListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("login_type", SHARE_MEDIA.SINA.toString());
                    confirmListener.confirmed(bundle);
                }
            }
        });

        wxLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                if (confirmListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("login_type", SHARE_MEDIA.WEIXIN.toString());
                    confirmListener.confirmed(bundle);
                }
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.5f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    public static int time = 60;

    public static void showBindPhoneDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.bind_phone_layout, null);
        final TextView bindBtn = (TextView) view.findViewById(R.id.bind_btn);
        ImageView closeIV = (ImageView) view.findViewById(R.id.close_iv);

        final EditText phoneET = (EditText) view.findViewById(R.id.phone_et);
        final EditText passwordET = (EditText) view.findViewById(R.id.password_et);
        final EditText codeET = (EditText) view.findViewById(R.id.code_et);
        final TextView timeGetTV = (TextView) view.findViewById(R.id.time_get_tv);

        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

        bindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneStr = phoneET.getText().toString();
                String passwordStr = passwordET.getText().toString();
                String codeStr = codeET.getText().toString();

                doLoginOrRegist(context, dialog, "bind", phoneStr, passwordStr, codeStr, confirmListener);
            }
        });

        // 获取验证码
        timeGetTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    String phoneStr = phoneET.getText().toString();
                    if (TextUtils.isEmpty(phoneStr)) {
                        ToastUtils.show(context, "手机号不能为空");
                        return;
                    } else if (phoneStr.length() != 11) {
                        ToastUtils.show(context, "请输入正确的手机号");
                        return;
                    }
                    time = 60;
                    timeGetTV.setEnabled(false);
                    Timer timer = new Timer();
                    TimerTask timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            KLog.d("lesleixz", "timer count");
                            if (time > 0) {
                                time--;

                                context.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        timeGetTV.setText(time + "s");
                                    }
                                });
                            } else {

                                cancel();

                                context.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        timeGetTV.setEnabled(true);
                                        timeGetTV.setText("重新获取");
                                    }
                                });
                            }

                        }
                    };

                    timer.scheduleAtFixedRate(timerTask, 0, 1000);

                    Bundle bundle = new Bundle();
                    bundle.putString("phone", phoneStr);
                    bundle.putString("type", "code");
                    confirmListener.confirmed(bundle);
                }
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.5f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    public static void showModifyPwdDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.modify_pwd_layout, null);
        final TextView confirmBtn = (TextView) view.findViewById(R.id.confirm_btn);
        ImageView closeIV = (ImageView) view.findViewById(R.id.close_iv);


        final EditText currentPwdET = (EditText) view.findViewById(R.id.current_pwd_et);
        final EditText newPwdET = (EditText) view.findViewById(R.id.new_pwd_et);
        final EditText reinputNewPwdET = (EditText) view.findViewById(R.id.reinput_new_pwd_et);


        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentPwdStr = currentPwdET.getText().toString();
                String newPwdStr = newPwdET.getText().toString();
                String reinputNewPwdStr = reinputNewPwdET.getText().toString();

                if (currentPwdStr.length() < 6) {
                    ToastUtils.show(context, "原密码不正确");
                    return;
                }

                if (newPwdStr.length() < 6) {
                    ToastUtils.show(context, "新密码长度不能小于6位");
                    return;
                }
                if (!TextUtils.equals(newPwdStr, reinputNewPwdStr)) {
                    ToastUtils.show(context, "两次输入的新密码不一致");
                    return;
                }

                if (confirmListener != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("password", currentPwdStr);
                    bundle.putString("new_password", newPwdStr);
                    confirmListener.confirmed(bundle);
                }

                dialog.cancel();
            }
        });


        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.5f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    public static void showSimpleDialog(final Activity context, String content, final ConfirmListener confirmListener) {
        showOneBtnDialog(context, "温馨提示", content, "知道了", confirmListener);
    }

    public static void showOneBtnDialog(final Activity context, String title, String content, String btnText,
                                        final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.daily_dialog, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView contentTV = (TextView) view.findViewById(R.id.inspiration_tv);
        TextView closeBtn = (TextView) view.findViewById(R.id.close_btn);

        if (!TextUtils.isEmpty(title))
            titleTV.setText(title);
        if (!TextUtils.isEmpty(content))
            contentTV.setText(content);
        if (!TextUtils.isEmpty(btnText))
            closeBtn.setText(btnText);

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                if (confirmListener != null) {
                    confirmListener.confirmed(null);
                }
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

    public static void imagePickerDialog(final MainActivity context, final SelectPictureListener selectPictureInterface) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.picture_select_way_layout, null);
        Button takePhotoBtn = (Button) view.findViewById(R.id.take_photo_btn);
        Button photoAlbumBtn = (Button) view.findViewById(R.id.photo_album_btn);
        Button cancelBtn = (Button) view.findViewById(R.id.cancel);

        takePhotoBtn.setOnClickListener(new View.OnClickListener() {//拍照
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                selectPictureInterface.takePhoto();
                dialog.dismiss();
            }
        });
        photoAlbumBtn.setOnClickListener(new View.OnClickListener() {//手机相册

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                selectPictureInterface.takePhotoAlbum();
                dialog.dismiss();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {//取消

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);
        Window window = dialog.getWindow();
//		window.setWindowAnimations(R.anim.push_bottom_in);
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle); // 添加动画

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    public static Dialog showLoadingDialog(final MainActivity context, String message) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.loading_layout, null);
        TextView messageTV = (TextView) view.findViewById(R.id.message_tv);
        messageTV.setText(message);

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
        return dialog;
    }

    public static void showExitLoginDialog(final Activity context, final ConfirmListener confirmListener) {
        final Dialog dialog = new Dialog(context, R.style.customDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.exit_login_layout, null);
        TextView closeBtn = (TextView) view.findViewById(R.id.cancel_tv);
        TextView confirmBtn = (TextView) view.findViewById(R.id.confirm_tv);

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                if (confirmListener != null) {
                    confirmListener.confirmed(null);
                }
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 2.0f / 3);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);


        dialog.show();
    }

}
